﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using InTheHand.Net; // e.g. BluetoothAddress, BluetoothEndPoint etc
using InTheHand.Net.Sockets; // e.g. BluetoothDeviceInfo, BluetoothClient, BluetoothListener
using InTheHand.Net.Bluetooth; // e.g. BluetoothService, BluetoothRadio
using System.Windows.Forms.DataVisualization.Charting;

namespace RLGDashboard
{
    public partial class RLGDashboard : Form
    {
        private BluetoothConnection bluetoothHandler;
        private EngineeringReplay replayHandler;
        private MonitorAllSensors allSensors;
        private InQueue transmissionQueue;
        private InQueue recordingQueue;
        private Thread updateDisplayThread;
        private bool recording = false;
        private EngineeringRecord record;
        private bool simulation;
        private LoadScreen loadScreenForm;
        private bool terminated;
        private ChartingQueue[] temperatureChartQueue = new ChartingQueue[4];
        private ChartingQueue[] pressureChartQueue = new ChartingQueue[4];
        private ChartingQueue[] rpmChartQueue = new ChartingQueue[4];
        private ChartingQueue afrChartQueue;
        private ChartingQueue voltageChartQueue;
        int i = 0;

        object[] egt1AllTime = new object[60000];
        private bool displayLiveGauges;
        private bool displayCharts;

        // Constructor if using an actual bluetooth connection
        public RLGDashboard(BluetoothConnection bluetoothHandler, LoadScreen loadScreenForm)
        {
            simulation = false;
            this.bluetoothHandler = bluetoothHandler;
            this.loadScreenForm = loadScreenForm;

            InitializeComponent();
            initilazeObjects();
        }

        //Constructor if using a simulation file to replay.
        public RLGDashboard(EngineeringReplay replayHandler, LoadScreen loadScreenForm)
        {
            simulation = true;
            this.replayHandler = replayHandler;
            this.loadScreenForm = loadScreenForm;

            InitializeComponent();
            initializeEGTRegions();
            initializeRPMRegions();
            initializePSIRegions();
            initializeAFRRegions();
            initializeVoltageRegions();
            initilazeObjects();
        }

        //
        public void initilazeObjects()
        {
            terminated = false;

            transmissionQueue = new InQueue();
            recordingQueue = new InQueue();

            displayLiveGauges = true;
            displayCharts = true;

            initializeChartQueues();

            //Spawn a new instance of MonitorAllSensors that will hold our sensor values, and spawn a thread to constantly
            //monitor the new queue.
            allSensors = new MonitorAllSensors(ref transmissionQueue);

            if (!simulation)
            {
                //Engineering build logic.  In final build, only the code for bluetoothHandler will remain.
                bluetoothHandler.monitorTransmissions(ref transmissionQueue);
            }
            else
            {
                replayHandler.simulateBluetooth(ref transmissionQueue);
            }

            updateDisplayThread = new Thread(new ThreadStart(updateDisplay));
            updateDisplayThread.Start();
        }
        
        private void initializeChartQueues()
        {
            temperatureChartQueue[0] = new ChartingQueue();
            temperatureChartQueue[1] = new ChartingQueue();
            temperatureChartQueue[2] = new ChartingQueue();
            temperatureChartQueue[3] = new ChartingQueue();

            pressureChartQueue[0] = new ChartingQueue();
            pressureChartQueue[1] = new ChartingQueue();
            pressureChartQueue[2] = new ChartingQueue();

            rpmChartQueue[0] = new ChartingQueue();
            rpmChartQueue[1] = new ChartingQueue();
            rpmChartQueue[2] = new ChartingQueue();

            afrChartQueue = new ChartingQueue();
            voltageChartQueue = new ChartingQueue();
        }
        public void updateDisplay()
        {
            EventWaitHandle waitHandle = new AutoResetEvent(false);

            // Wait 250 milliseconds on initial form loading to prevent the thread from trying to update values before the
            // form has been created.
            waitHandle.WaitOne(250);

            while (updateDisplayThread.IsAlive)
            {
                DateTime start = DateTime.Now;

                updateTemperatureDisplay();
                updatePsiDisplay();
                updateRpmDisplay();
                updateAfrDisplay();
                updateVoltageDisplay();

                i++;
                DateTime end = DateTime.Now;
                TimeSpan span = end - start;

                if (span.Milliseconds < 124)
                {
                    // 8 Hz = 1/8 = 125ms, WaitOne has inherent 1ms cost.
                    waitHandle.WaitOne(124 - span.Milliseconds);
                }
            }
        }
        private void updateTemperatureDisplay()
        {
            object[] sensors = allSensors.tempSensors.getValues();
          
            IAsyncResult result = BeginInvoke(new MethodInvoker(delegate()
            {
                if (displayLiveGauges)
                {
                    updateTempGauges(sensors);
                }

                //Logic for Display Charts handled inside.
                updateTempChart(sensors);
                
            }));
        }
        private void updateTempGauges(object[] tempSensors)
        {
            updateTemperatureGauge(tempSensors[0], aEGT1);
            updateTemperatureGauge(tempSensors[1], aEGT2);
            updateTemperatureGauge(tempSensors[2], aEGT3);
            updateTemperatureGauge(tempSensors[3], aEGT4);
        }
        private void updateTemperatureGauge(object sensorValue, AGauge tempGauge)
        {
            if (Convert.ToInt16(sensorValue) != -5865)
            {
                tempGauge.Value = Convert.ToSingle(sensorValue);
                tempGauge.CapText = sensorValue.ToString() + " F";
            }
            else
            {
                tempGauge.Value = 0;
                tempGauge.CapText = "ERR";
            }
        }

        private void updateTempChart(object[] tempSensors)
        {
            aEGTChart.Series[Properties.Settings.Default.egt1Name].Points.Clear();
            aEGTChart.Series[Properties.Settings.Default.egt2Name].Points.Clear();
            aEGTChart.Series[Properties.Settings.Default.egt3Name].Points.Clear();
            aEGTChart.Series[Properties.Settings.Default.egt4Name].Points.Clear();


            for (int index = 0; index < 4; index++)
            {
                if (Convert.ToInt16(tempSensors[index]) != -5865)
                {
                    temperatureChartQueue[index].addNode(tempSensors[index]);
                }
                else
                {
                    temperatureChartQueue[index].addNode("");
                }
            }

            if (displayCharts)
            {
                temperatureChartQueue[0].DrawPoints(aEGTChart, Properties.Settings.Default.egt1Name);
                temperatureChartQueue[1].DrawPoints(aEGTChart, Properties.Settings.Default.egt2Name);
                temperatureChartQueue[2].DrawPoints(aEGTChart, Properties.Settings.Default.egt3Name);
                temperatureChartQueue[3].DrawPoints(aEGTChart, Properties.Settings.Default.egt4Name);
            }
        }
        private void updatePsiDisplay()
        {
            IAsyncResult result = BeginInvoke(new MethodInvoker(delegate()
            {
                //Get string value for sensors
                object[] sensors = allSensors.psiSensors.getValues();

                if (displayLiveGauges)
                {
                    updatePSIGauges(sensors);
                }
                
                //Logic for displayCharts handled inside
                updatePSIChart(sensors);
                
            }));
        }

        private void updatePSIGauges(object[] tempSensors)
        {
            aPSI1.Value = Convert.ToSingle(tempSensors[0]);
            aPSI1.CapText = tempSensors[0].ToString();
            aPSI1.CapsText[1] = Properties.Settings.Default.psi1Name;

            aPSI2.Value = Convert.ToSingle(tempSensors[1]);
            aPSI2.CapText = tempSensors[1].ToString();
            aPSI2.CapsText[1] = Properties.Settings.Default.psi2Name;

            aPSI3.Value = Convert.ToSingle(tempSensors[2]);
            aPSI3.CapText = tempSensors[2].ToString();
            aPSI3.CapsText[1] = Properties.Settings.Default.psi3Name;
        }
        private void updatePSIChart(object[] tempSensors)
        {
            aPSIChart.Series[Properties.Settings.Default.psi1Name].Points.Clear();
            aPSIChart.Series[Properties.Settings.Default.psi2Name].Points.Clear();
            aPSIChart.Series[Properties.Settings.Default.psi3Name].Points.Clear();

            for (int index = 0; index < 3; index++)
            {
                pressureChartQueue[index].addNode(tempSensors[index]);
            }

            if (displayCharts)
            {
                pressureChartQueue[0].DrawPoints(aPSIChart, Properties.Settings.Default.psi1Name);
                pressureChartQueue[1].DrawPoints(aPSIChart, Properties.Settings.Default.psi2Name);
                pressureChartQueue[2].DrawPoints(aPSIChart, Properties.Settings.Default.psi3Name);
            }
        }
        private void updateRpmDisplay()
        {
            IAsyncResult result = BeginInvoke(new MethodInvoker(delegate()
            {
                //Get string value for sensors
                object[] sensors = allSensors.rpmSensors.getValues();

                if (displayLiveGauges)
                {
                    updateRPMGraphs(sensors);
                }
                
                //Logic for displayCharts handled inside
                updateRPMChart(sensors);
            }));
        }

        private void updateRPMGraphs(object[] sensors)
        {

            aRPM1.Value = Convert.ToSingle(sensors[0]);
            aRPM1.CapText = sensors[0].ToString();

            aRPM2.Value = Convert.ToSingle(sensors[1]);
            aRPM2.CapText = sensors[1].ToString();

            aRPM3.Value = Convert.ToSingle(sensors[2]);
            aRPM3.CapText = sensors[2].ToString();
        }

        private void updateRPMChart(object[] sensors)
        {
            aRPMChart.Series[Properties.Settings.Default.rpm1Name].Points.Clear();
            aRPMChart.Series[Properties.Settings.Default.rpm2Name].Points.Clear();
            aRPMChart.Series[Properties.Settings.Default.rpm3Name].Points.Clear();

            for (int index = 0; index < 3; index++)
            {
                rpmChartQueue[index].addNode(sensors[index]);
            }

            if (displayCharts)
            {
                rpmChartQueue[0].DrawPoints(aRPMChart, Properties.Settings.Default.rpm1Name);
                rpmChartQueue[1].DrawPoints(aRPMChart, Properties.Settings.Default.rpm2Name);
                rpmChartQueue[2].DrawPoints(aRPMChart, Properties.Settings.Default.rpm3Name);
            }
        }
        private void updateVoltageDisplay()
        {
            IAsyncResult result = BeginInvoke(new MethodInvoker(delegate()
            {
                //Get string value for sensors
                object[] sensors = allSensors.voltageSensors.getValues();

                if (displayLiveGauges)
                {
                    updateVoltageGraphs(sensors);
                }
                //Logic for displaycharts handled inside
                updateVoltageChart(sensors);
            }));
        }

        private void updateVoltageGraphs(object[] sensors)
        {
            aVoltage.Value = Convert.ToSingle(sensors[0]);
            aVoltage.CapText = Math.Round(Convert.ToSingle(sensors[0]), 1, MidpointRounding.ToEven).ToString() + " V";
        }
        private void updateVoltageChart(object[] sensors)
        {
            aVoltageChart.Series[Properties.Settings.Default.voltageName].Points.Clear();
            aVoltageChart.Series[Properties.Settings.Default.voltageName].Color = Color.FromName(Properties.Settings.Default.voltageLineColor);

            voltageChartQueue.addNode(sensors[0]);

            if (displayCharts)
            {
                voltageChartQueue.DrawPoints(aVoltageChart, Properties.Settings.Default.voltageName);
            }
        }

        private void updateAfrDisplay()
        {
            IAsyncResult result = BeginInvoke(new MethodInvoker(delegate()
            {
                //Get string value for sensors
                object[] sensors = allSensors.afrSensors.getValues();

                if (displayLiveGauges)
                {
                    updateAFRGraphs(sensors);
                }
                updateAFRChart(sensors);
            }));
        }

        private void updateAFRGraphs(object[] sensors)
        {
            if (sensors[0].ToString() != "ERR")
            {
                aAfr.Value = Convert.ToSingle(sensors[0]);
                aAfr.CapText = sensors[0].ToString() + "%";
            }
            else
            {
                aAfr.Value = 0;
                aAfr.CapText = "ERR";
            }
        }

        private void updateAFRChart(object[] sensors)
        {
            aAFRChart.Series[Properties.Settings.Default.afrName].Points.Clear();

            if (sensors[0].ToString() != "ERR")
            {
                afrChartQueue.addNode(sensors[0]);
            }
            else
            {
                afrChartQueue.addNode("");
            }

            if (displayCharts)
            {
                afrChartQueue.DrawPoints(aAFRChart, Properties.Settings.Default.afrName);
            }
        }
        private void terminateConnection_Click(object sender, EventArgs e)
        {
            allSensors.terminateListenThread();

            if (!simulation)
            {
                bluetoothHandler.killConnection();
            }
            else
            {
                replayHandler.killSimulation();
            }
            updateDisplayThread.Abort();
            returnLoadScreen();

            //Boolean to show that the terminated buttonw as clicked.
            terminated = true;

            this.Close();
        }

        private void returnLoadScreen()
        {
            loadScreenForm.Visible = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!simulation)
            {
                if (bluetoothHandler.isConnected())
                {
                    allSensors.terminateListenThread();
                    bluetoothHandler.killConnection();

                    updateDisplayThread.Abort();
                }
            }
            else
            {
                if (replayHandler.isSimulating())
                {
                    allSensors.terminateListenThread();
                    replayHandler.killSimulation();

                    updateDisplayThread.Abort();
                }
            }

            //If they pressed the red X, they are killing the application.  If they pressed terminate, return to load screen.
            if (!terminated)
            {
                loadScreenForm.Close();
            }
        }

        private void engineeringRecord_Click(object sender, EventArgs e)
        {
            if (recording == false)
            {
                recording = true;
                engineeringRecord.Text = "Stop Recording";

                record = new EngineeringRecord();
                record.startRecording(ref recordingQueue);
                bluetoothHandler.addRecordingQueue(ref recordingQueue);
            }

            else if (recording == true)
            {
                recording = false;
                engineeringRecord.Text = "Record";

                record.stopRecording();
                bluetoothHandler.stopRecording();
            }
        }

        private void rdoLiveGauges_CheckedChanged(object sender, EventArgs e)
        {
            flipDisplay(true);
        }

        private void rdoCharts_CheckedChanged(object sender, EventArgs e)
        {
            flipDisplay(false);
        }
        private void flipDisplay(bool boolean)
        {
            aEGT1.Visible = boolean;
            aEGT2.Visible = boolean;
            aEGT3.Visible = boolean;
            aEGT4.Visible = boolean;

            aPSI1.Visible = boolean;
            aPSI2.Visible = boolean;
            aPSI3.Visible = boolean;

            aRPM1.Visible = boolean;
            aRPM2.Visible = boolean;
            aRPM3.Visible = boolean;

            aAfr.Visible = boolean;
            aVoltage.Visible = boolean;

            aEGTChart.Visible = !boolean;
            aPSIChart.Visible = !boolean;
            aRPMChart.Visible = !boolean;
            aAFRChart.Visible = !boolean;
            aVoltageChart.Visible = !boolean;

            displayLiveGauges = boolean;
            displayCharts = !boolean;
        }

        private void launchConfigScreen_Click(object sender, EventArgs e)
        {
            GUIConfiguration form = new GUIConfiguration();
            form.Show();
        }

        private void configurationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String[] names = {Properties.Settings.Default.voltageName, Properties.Settings.Default.rpm1Name, Properties.Settings.Default.rpm2Name, Properties.Settings.Default.rpm3Name,
                                 Properties.Settings.Default.psi1Name, Properties.Settings.Default.psi2Name, Properties.Settings.Default.psi3Name, Properties.Settings.Default.egt1Name,
                                 Properties.Settings.Default.egt2Name, Properties.Settings.Default.egt3Name, Properties.Settings.Default.egt4Name,Properties.Settings.Default.afrName};

            GUIConfiguration form = new GUIConfiguration();
            form.ShowDialog();
            
            updateGUI(names);
        }

        private void updateGUI(String[] names)
        {
            //Update Voltage Stuff and Thangs
            aVoltageChart.Series[names[0]].Color = Color.FromName(Properties.Settings.Default.voltageLineColor);
            aVoltageChart.Series.FindByName(names[0]).Name = Properties.Settings.Default.voltageName;
            aVoltage.MaxValue = Properties.Settings.Default.voltageMax;
            aVoltage.MinValue = Properties.Settings.Default.voltageMin;
            aVoltage.CapsText[0] = Properties.Settings.Default.voltageName;
            
            //Update RPM Stuff and Thangs
            aRPMChart.Series[names[1]].Color = Color.FromName(Properties.Settings.Default.rpmLine1Color);
            aRPMChart.Series.FindByName(names[1]).Name = Properties.Settings.Default.rpm1Name;
            aRPM1.MaxValue = Properties.Settings.Default.rpm1Max;
            aRPM1.MinValue = Properties.Settings.Default.rpm1Min;
            aRPM1.CapsText[1] = Properties.Settings.Default.rpm1Name;

            aRPMChart.Series[names[2]].Color = Color.FromName(Properties.Settings.Default.rpmLine2Color);
            aRPMChart.Series.FindByName(names[2]).Name = Properties.Settings.Default.rpm2Name;
            aRPM2.MaxValue = Properties.Settings.Default.rpm2Max;
            aRPM2.MinValue = Properties.Settings.Default.rpm2Min;
            aRPM2.CapsText[1] = Properties.Settings.Default.rpm2Name;

            aRPMChart.Series[names[3]].Color = Color.FromName(Properties.Settings.Default.rpmLine3Color);
            aRPMChart.Series.FindByName(names[3]).Name = Properties.Settings.Default.rpm3Name;
            aRPM3.MaxValue = Properties.Settings.Default.rpm3Max;
            aRPM3.MinValue = Properties.Settings.Default.rpm3Min;
            aRPM3.CapsText[1] = Properties.Settings.Default.rpm3Name;

            //Update PSI Stuff and Thangs
            aPSIChart.Series[names[4]].Color = Color.FromName(Properties.Settings.Default.psiLine1Color);
            aPSIChart.Series.FindByName(names[4]).Name = Properties.Settings.Default.psi1Name;
            aPSI1.MaxValue = Properties.Settings.Default.psi1Max;
            aPSI1.MinValue = Properties.Settings.Default.psi1Min;
            aPSI1.CapsText[1] = Properties.Settings.Default.psi1Name;

            aPSIChart.Series[names[5]].Color = Color.FromName(Properties.Settings.Default.psiLine2Color);
            aPSIChart.Series.FindByName(names[5]).Name = Properties.Settings.Default.psi2Name;
            aPSI2.MaxValue = Properties.Settings.Default.psi2Max;
            aPSI2.MinValue = Properties.Settings.Default.psi2Min;
            aPSI2.CapsText[1] = Properties.Settings.Default.psi2Name;

            aPSIChart.Series[names[6]].Color = Color.FromName(Properties.Settings.Default.psiLine3Color);
            aPSIChart.Series.FindByName(names[6]).Name = Properties.Settings.Default.psi3Name;
            aPSI3.MaxValue = Properties.Settings.Default.psi3Max;
            aPSI3.MinValue = Properties.Settings.Default.psi3Min;
            aPSI3.CapsText[1] = Properties.Settings.Default.psi3Name;

            //Update EGT Stuff and Thangs
            aEGTChart.Series[names[7]].Color = Color.FromName(Properties.Settings.Default.egtLine1Color);
            aEGTChart.Series.FindByName(names[7]).Name = Properties.Settings.Default.egt1Name;
            aEGT1.MaxValue = Properties.Settings.Default.egt1Max;
            aEGT1.MinValue = Properties.Settings.Default.egt1Min;
            aEGT1.CapsText[1] = Properties.Settings.Default.egt1Name;

            aEGTChart.Series[names[8]].Color = Color.FromName(Properties.Settings.Default.egtLine2Color);
            aEGTChart.Series.FindByName(names[8]).Name = Properties.Settings.Default.egt2Name;
            aEGT2.MaxValue = Properties.Settings.Default.egt2Max;
            aEGT2.MinValue = Properties.Settings.Default.egt2Min;
            aEGT2.CapsText[1] = Properties.Settings.Default.egt2Name;

            aEGTChart.Series[names[9]].Color = Color.FromName(Properties.Settings.Default.egtLine3Color);
            aEGTChart.Series.FindByName(names[9]).Name = Properties.Settings.Default.egt3Name;
            aEGT3.MaxValue = Properties.Settings.Default.egt3Max;
            aEGT3.MinValue = Properties.Settings.Default.egt3Min;
            aEGT3.CapsText[1] = Properties.Settings.Default.egt3Name;

            aEGTChart.Series[names[10]].Color = Color.FromName(Properties.Settings.Default.egtLine4Color);
            aEGTChart.Series.FindByName(names[10]).Name = Properties.Settings.Default.egt4Name;
            aEGT4.MaxValue = Properties.Settings.Default.egt4Max;
            aEGT4.MinValue = Properties.Settings.Default.egt4Min;
            aEGT4.CapsText[1] = Properties.Settings.Default.egt4Name;

            //Update AFR Stuff and Thangs
            aAFRChart.Series[names[11]].Color = Color.FromName(Properties.Settings.Default.afrLineColor);
            aAFRChart.Series.FindByName(names[11]).Name = Properties.Settings.Default.afrName;
            aAfr.MaxValue = Properties.Settings.Default.afrMax;
            aAfr.MinValue = Properties.Settings.Default.afrMin;
            aAfr.CapsText[1] = Properties.Settings.Default.afrName;

            //Update Ranges
            this.ranges = xml.retrieveFromXml();
            initializeEGTRegions();
            initializeRPMRegions();
            initializePSIRegions();
            initializeAFRRegions();
            initializeVoltageRegions();

            this.Refresh();
        }

        public void initializeEGTRegions()
        {
            for (i = 0; i < 5; i++)
            {
                if (i < ranges.egt1Container.Length)
                {
                    this.aEGT1.RangesStartValue[i] = ranges.egt1Container[i].min;
                    this.aEGT1.RangesEndValue[i] = ranges.egt1Container[i].max;
                    this.aEGT1.RangesEnabled[i] = true;
                    this.aEGT1.RangesColor[i] = System.Drawing.Color.FromName(ranges.egt1Container[i].color);
                }
                else
                {
                    this.aEGT1.RangesStartValue[i] = 0;
                    this.aEGT1.RangesEndValue[i] = 0;
                    this.aEGT1.RangesEnabled[i] = false;
                    this.aEGT1.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }

            for (i = 0; i < 5; i++)
            {
                if (i < ranges.egt2Container.Length)
                {
                    this.aEGT2.RangesStartValue[i] = ranges.egt2Container[i].min;
                    this.aEGT2.RangesEndValue[i] = ranges.egt2Container[i].max;
                    this.aEGT2.RangesEnabled[i] = true;
                    this.aEGT2.RangesColor[i] = System.Drawing.Color.FromName(ranges.egt2Container[i].color);
                }
                else
                {
                    this.aEGT2.RangesStartValue[i] = 0;
                    this.aEGT2.RangesEndValue[i] = 0;
                    this.aEGT2.RangesEnabled[i] = false;
                    this.aEGT2.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }

            for (i = 0; i < 5; i++)
            {
                if (i < ranges.egt3Container.Length)
                {
                    this.aEGT3.RangesStartValue[i] = ranges.egt3Container[i].min;
                    this.aEGT3.RangesEndValue[i] = ranges.egt3Container[i].max;
                    this.aEGT3.RangesEnabled[i] = true;
                    this.aEGT3.RangesColor[i] = System.Drawing.Color.FromName(ranges.egt3Container[i].color);
                }
                else
                {
                    this.aEGT3.RangesStartValue[i] = 0;
                    this.aEGT3.RangesEndValue[i] = 0;
                    this.aEGT3.RangesEnabled[i] = false;
                    this.aEGT3.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }

            for (i = 0; i < 5; i++)
            {
                if (i < ranges.egt4Container.Length)
                {
                    this.aEGT4.RangesStartValue[i] = ranges.egt4Container[i].min;
                    this.aEGT4.RangesEndValue[i] = ranges.egt4Container[i].max;
                    this.aEGT4.RangesEnabled[i] = true;
                    this.aEGT4.RangesColor[i] = System.Drawing.Color.FromName(ranges.egt4Container[i].color);
                }
                else
                {
                    this.aEGT4.RangesStartValue[i] = 0;
                    this.aEGT4.RangesEndValue[i] = 0;
                    this.aEGT4.RangesEnabled[i] = false;
                    this.aEGT4.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }
        }

        public void initializeRPMRegions()
        {
            for (i = 0; i < 5; i++)
            {
                if (i < ranges.rpm1Container.Length)
                {
                    this.aRPM1.RangesStartValue[i] = ranges.rpm1Container[i].min;
                    this.aRPM1.RangesEndValue[i] = ranges.rpm1Container[i].max;
                    this.aRPM1.RangesEnabled[i] = true;
                    this.aRPM1.RangesColor[i] = System.Drawing.Color.FromName(ranges.rpm1Container[i].color);
                }
                else
                {
                    this.aRPM1.RangesStartValue[i] = 0;
                    this.aRPM1.RangesEndValue[i] = 0;
                    this.aRPM1.RangesEnabled[i] = false;
                    this.aRPM1.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }

            for (i = 0; i < 5; i++)
            {
                if (i < ranges.rpm2Container.Length)
                {
                    this.aRPM2.RangesStartValue[i] = ranges.rpm2Container[i].min;
                    this.aRPM2.RangesEndValue[i] = ranges.rpm2Container[i].max;
                    this.aRPM2.RangesEnabled[i] = true;
                    this.aRPM2.RangesColor[i] = System.Drawing.Color.FromName(ranges.rpm2Container[i].color);
                }
                else
                {
                    this.aRPM2.RangesStartValue[i] = 0;
                    this.aRPM2.RangesEndValue[i] = 0;
                    this.aRPM2.RangesEnabled[i] = false;
                    this.aRPM2.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }

            for (i = 0; i < 5; i++)
            {
                if (i < ranges.rpm3Container.Length)
                {
                    this.aRPM3.RangesStartValue[i] = ranges.rpm3Container[i].min;
                    this.aRPM3.RangesEndValue[i] = ranges.rpm3Container[i].max;
                    this.aRPM3.RangesEnabled[i] = true;
                    this.aRPM3.RangesColor[i] = System.Drawing.Color.FromName(ranges.rpm3Container[i].color);
                }
                else
                {
                    this.aRPM3.RangesStartValue[i] = 0;
                    this.aRPM3.RangesEndValue[i] = 0;
                    this.aRPM3.RangesEnabled[i] = false;
                    this.aRPM3.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }
        }

        public void initializePSIRegions()
        {
            for (i = 0; i < 5; i++)
            {
                if (i < ranges.psi1Container.Length)
                {
                    this.aPSI1.RangesStartValue[i] = ranges.psi1Container[i].min;
                    this.aPSI1.RangesEndValue[i] = ranges.psi1Container[i].max;
                    this.aPSI1.RangesEnabled[i] = true;
                    this.aPSI1.RangesColor[i] = System.Drawing.Color.FromName(ranges.psi1Container[i].color);
                }
                else
                {
                    this.aPSI1.RangesStartValue[i] = 0;
                    this.aPSI1.RangesEndValue[i] = 0;
                    this.aPSI1.RangesEnabled[i] = false;
                    this.aPSI1.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }

            for (i = 0; i < 5; i++)
            {
                if (i < ranges.psi2Container.Length)
                {
                    this.aPSI2.RangesStartValue[i] = ranges.psi2Container[i].min;
                    this.aPSI2.RangesEndValue[i] = ranges.psi2Container[i].max;
                    this.aPSI2.RangesEnabled[i] = true;
                    this.aPSI2.RangesColor[i] = System.Drawing.Color.FromName(ranges.psi2Container[i].color);
                }
                else
                {
                    this.aPSI2.RangesStartValue[i] = 0;
                    this.aPSI2.RangesEndValue[i] = 0;
                    this.aPSI2.RangesEnabled[i] = false;
                    this.aPSI2.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }

            for (i = 0; i < 5; i++)
            {
                if (i < ranges.psi3Container.Length)
                {
                    this.aPSI3.RangesStartValue[i] = ranges.psi3Container[i].min;
                    this.aPSI3.RangesEndValue[i] = ranges.psi3Container[i].max;
                    this.aPSI3.RangesEnabled[i] = true;
                    this.aPSI3.RangesColor[i] = System.Drawing.Color.FromName(ranges.psi3Container[i].color);
                }
                else
                {
                    this.aPSI3.RangesStartValue[i] = 0;
                    this.aPSI3.RangesEndValue[i] = 0;
                    this.aPSI3.RangesEnabled[i] = false;
                    this.aPSI3.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }
        }

        public void initializeVoltageRegions()
        {
            for (i = 0; i < 5; i++)
            {
                if (i < ranges.afrContainer.Length)
                {
                    this.aAfr.RangesStartValue[i] = ranges.afrContainer[i].min;
                    this.aAfr.RangesEndValue[i] = ranges.afrContainer[i].max;
                    this.aAfr.RangesEnabled[i] = true;
                    this.aAfr.RangesColor[i] = System.Drawing.Color.FromName(ranges.afrContainer[i].color);
                }
                else
                {
                    this.aAfr.RangesStartValue[i] = 0;
                    this.aAfr.RangesEndValue[i] = 0;
                    this.aAfr.RangesEnabled[i] = false;
                    this.aAfr.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }
        }

        public void initializeAFRRegions()
        {
            for (i = 0; i < 5; i++)
            {
                if (i < ranges.voltageContainer.Length)
                {
                    this.aVoltage.RangesStartValue[i] = ranges.voltageContainer[i].min;
                    this.aVoltage.RangesEndValue[i] = ranges.voltageContainer[i].max;
                    this.aVoltage.RangesEnabled[i] = true;
                    this.aVoltage.RangesColor[i] = System.Drawing.Color.FromName(ranges.voltageContainer[i].color);
                }
                else
                {
                    this.aVoltage.RangesStartValue[i] = 0;
                    this.aVoltage.RangesEndValue[i] = 0;
                    this.aVoltage.RangesEnabled[i] = false;
                    this.aVoltage.RangesColor[i] = System.Drawing.Color.FromName("Black");
                }
            }
        }
    }
}
